home *** CD-ROM | disk | FTP | other *** search
/ MaxiMac 2000 December / MaxiMac 109.iso / Macworld on CD n°109 / Applications (Mac OS X PB) / MacOSX ScreenSavers / Source Code / Icon / IconSaver.m < prev    next >
Encoding:
Text File  |  2000-08-01  |  4.4 KB  |  189 lines  |  [????/????]

  1. /* IconSaver.m created by epeyton on Fri 17-Sep-1999 */
  2.  
  3. #import "IconSaver.h"
  4.  
  5. @implementation IconSaver
  6.  
  7. - (void)drawRect:(NSRect)rects
  8. {
  9.     [[NSColor blackColor] set];
  10.     NSRectFill(rects);
  11. }
  12.  
  13. - (id)initWithFrame:(NSRect)frameRect
  14. {    
  15.     if (![super initWithFrame:frameRect]) return nil;
  16.     
  17.     iconImageArray = [[NSMutableArray alloc] init];
  18.     
  19.     [NSBundle loadNibNamed:@"Icon" owner:self];
  20.  
  21.  
  22.     [self allocateGState];
  23.     srandom(time(0));
  24.  
  25.     directoryArray = [[self getAvailableIconDirectories:NO] retain];
  26.     
  27.     return self;
  28. }
  29.  
  30. - (void)setFrameSize:(NSSize)newSize
  31. {
  32.     if ((newSize.width > 640) && (newSize.width > 480)) {
  33.     canDraw = YES;
  34.     } else {
  35.     canDraw = NO;
  36.     }
  37.     width = newSize.width;
  38.     height = newSize.height;
  39.     [super setFrameSize:newSize];
  40.     return;
  41. }
  42.  
  43.     
  44.  
  45. - (NSTimeInterval)animationTimeInterval
  46. {
  47.     return 0.1;
  48. }
  49.  
  50. - (void)recreateIconPaths:sender
  51. {
  52.     [self getAvailableIconDirectories:YES];
  53. }
  54.  
  55.  
  56. - (NSArray *)getAvailableIconDirectories:(BOOL)recreate
  57. {
  58.     NSArray *retArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"IconPathArray"];
  59.  
  60.     if (!retArray  || recreate) {
  61.         NSMutableArray *mutArray = [NSMutableArray array];
  62.     
  63.         // search /Local/Applications
  64.         [mutArray addObjectsFromArray:[self arrayAtLocation:@"/Applications"]];
  65.         // search /System/Developer/Applications
  66.         [mutArray addObjectsFromArray:[self arrayAtLocation:@"/Developer/Applications"]];
  67.         // search ~/Applications
  68.         [mutArray addObjectsFromArray:[self arrayAtLocation:[NSString stringWithFormat:@"%@/%@", NSHomeDirectory(),@"Applications"]]];
  69.  
  70.         retArray = [NSArray arrayWithArray:mutArray];
  71.  
  72.         [[NSUserDefaults standardUserDefaults] setObject:retArray forKey:@"IconPathArray"];
  73.     }
  74.     
  75.     return retArray;
  76. }
  77.  
  78. - (NSArray *)arrayAtLocation:(NSString *)location
  79. {
  80.     id file;
  81.     
  82.     NSMutableArray *mutArray = [NSMutableArray array];
  83.     NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager]
  84.           enumeratorAtPath:location];
  85.     while (file = [enumerator nextObject]) {
  86.         if ([[file pathExtension] isEqualToString:@"app"])
  87.              [mutArray addObject:[NSString stringWithFormat:@"%@/%@",location, file]];
  88.     }
  89.     return [NSArray arrayWithArray:mutArray];
  90. }
  91.  
  92. - (NSRect)randomRect
  93. {
  94.     BOOL found = NO;
  95.     NSRect currentRect;
  96.  
  97.     while (!found) {
  98.         // pick a random point and size
  99.         NSPoint point;
  100.         NSSize size;
  101.         int factor = SSRandomFloatBetween(1.0,5.0);
  102.         NSEnumerator *iconImageEnum;
  103.         id anIconImage;
  104.  
  105. pickNew:
  106.     iconImageEnum = [iconImageArray objectEnumerator];
  107.  
  108.         point = NSMakePoint(SSRandomFloatBetween(0.0, width),SSRandomFloatBetween(0.0, height));
  109.  
  110.         size = NSMakeSize(48*factor, 48*factor);
  111.  
  112.         currentRect = NSMakeRect(point.x, point.y, size.width, size.height);
  113.  
  114.         
  115.         // see if these fall into any of the rects already doled out
  116.         while ((anIconImage = [iconImageEnum nextObject])) {
  117.             if (NSIntersectsRect([anIconImage imageRect] , currentRect)) {
  118.                 goto pickNew;
  119.         
  120.             }
  121.         }
  122.         found = YES;
  123.     }
  124.  
  125.     // return the final rect (made from rect and size
  126.     return currentRect;
  127. }
  128.  
  129. - (void)oneStep
  130. {
  131.     NSEnumerator *iconImageEnum = [iconImageArray objectEnumerator];
  132.     id anIconImage;
  133.     
  134.  
  135.     
  136.     if (!canDraw) {
  137.     return;
  138.     }
  139.  
  140.     while (anIconImage = [iconImageEnum nextObject]) {
  141.         [anIconImage display];
  142.     }
  143.     
  144.     return;
  145. }
  146.  
  147. - (void)removeIconImage:(IconImage *)iconImage
  148. {
  149.  
  150.     // create a new image too
  151.     [iconImageArray addObject:[[[IconImage alloc] initWithPath:[directoryArray objectAtIndex:SSRandomFloatBetween(0, [directoryArray count] - 1)] inRect:[self randomRect] parent:self] autorelease]];
  152.  
  153.     [iconImageArray removeObject:iconImage];
  154. }
  155.  
  156. - (void) startAnimation
  157. {
  158.     int initialImages = SSRandomFloatBetween(8.0, 15.0);
  159.     int i = 0;
  160.     if (!canDraw) {
  161.     return;
  162.     }
  163.     
  164.  
  165.     for (i = 0;i < initialImages - 1;i++) {
  166.         [iconImageArray addObject:[[[IconImage alloc] initWithPath:[directoryArray objectAtIndex:SSRandomFloatBetween(0, [directoryArray count] - 1)] inRect:[self randomRect] parent:self] autorelease]];
  167.     }
  168.     [super startAnimation];
  169. }
  170.  
  171.  
  172. - (void) stopAnimation
  173. {
  174.  
  175.     [iconImageArray removeAllObjects];
  176.     [super stopAnimation];
  177. }
  178.  
  179. - (BOOL)hasConfigureSheet { return YES; }
  180. - (NSWindow*)configureSheet { return window; }
  181.  
  182. - (void)closeSheet:(id)ssender
  183. {
  184.     [NSApp endSheet:window];
  185. }
  186.  
  187.  
  188. @end
  189.